'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview VIP功能
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var moment = require('moment');
var _ = require('lodash');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var PlayerManager = require('../../../libs/playerManager');
var dataApi = require('../../../libs/dataApi');
var rewardService = require('../../../services/rewardService');
var propertyConf = require('../../../config/propertyConf');
var roleService = require('../../../services/roleService');
var vipService = require('../../../services/vipService');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 领取VIP奖励
 */
Handler.prototype.receiveReward = function (msg, session, next) {
    var role_id = session.uid;
    var vip_level = msg.vip_level;
    if (!vip_level) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'bag', 'vip', 'build', 'soldier', 'task', 'daily_task', 'accrued', 'achieve', 'tech', 'sevenDayActivity'],
                    cb
                );
            },
            function (cb) {
                var vip_model = player_manager.vip;
                var role_model = player_manager.role;

                // 是否已经领取过
                if (!vip_model.isCanReceive(vip_level)) {
                    return next(null, {
                        code: Code.USER_VIP_REWARD_NOT_RECIEVE
                    });
                }

                var vip = role_model.getVip();
                vip_model.addReceiveVipLevel(vip_level, vip);

                var reward = vipService.generateVipReward(vip_level, role_model.getJob());
                rewardService.addRewardListToRole(player_manager, reward, moduleType.MDL_OTHER, operationType.OPR_OTHER_VIP_LVL_REWARD);
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};

/**
 * 购买特权VIP宝箱
 */
Handler.prototype.buyVipBox = function (msg, session, next) {
    var role_id = session.uid;
    var property_type = msg.property_type;

    if (!property_type) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }

    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'bag', 'vip', 'build', 'task', 'daily_task', 'achieve', 'tech', 'sevenDayActivity', 'accrued'],
                    cb
                );
            },
            function (cb) {
                var vip_model = player_manager.vip;
                var role_model = player_manager.role;

                var vip_level = role_model.getVip();

                // 是否已经购买过
                if (!vip_model.isCanBuy(property_type)) {
                    return next(null, {
                        code: Code.USER_VIP_BOX_NOT_BUY
                    });
                }

                var property_conf = propertyConf.getPropertyTypeConf(property_type);

                // 扣除金币
                if (!roleService.subGold(player_manager, moduleType.MDL_OTHER, operationType.OPR_OTHER_VIP_BOX, property_conf.price)) {
                    return next(null, {
                        code: Code.USER_GOLD_NOT_ENOUGH
                    });
                }

                vip_model.addVipPackage(property_type);

                var reward = vipService.generateVipPackage(vip_level);
                rewardService.addRewardToRole(player_manager, reward, moduleType.MDL_OTHER, operationType.OPR_OTHER_VIP_BOX);

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};
